package com.ruoyi.business.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.business.model.CascaderModel;
import com.ruoyi.common.utils.RedisUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.DataServerMapper;
import com.ruoyi.business.domain.DataServer;
import com.ruoyi.business.service.IDataServerService;

import javax.annotation.PostConstruct;

import static com.ruoyi.business.constants.CacheConstant.DATA_SERVER_TYPE;

/**
 * 数据服务信息Service业务层处理
 *
 * @author Eric
 * @date 2024-04-10
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DataServerServiceImpl extends ServiceImpl<DataServerMapper, DataServer> implements IDataServerService {


    @PostConstruct
    public void init() {
        log.info("初始化图层数据类型数据.....");
        loadDataServerType();
    }

    public void loadDataServerType() {
        Map<String, Object> map = baseMapper.getDataMap();
        Map<String, Long> dataServerTypeMap = Maps.newHashMap();
        map.forEach((k, v) -> {
            Map<String, Object> vMap = Convert.toMap(String.class, Object.class, v);
//            log.info("map===k:{},v:{}", k, MapUtil.getInt(vMap, "serverTypeId"));
            dataServerTypeMap.put(k, MapUtil.getLong(vMap, "serverTypeId"));
        });
        RedisUtils.deleteObject(DATA_SERVER_TYPE);
        RedisUtils.setCacheObject(DATA_SERVER_TYPE, dataServerTypeMap);
    }

    /**
     * 自然岸线率
     * @param name
     * @return
     */
    private Long getTypeId(String name) {
        Map<String, Long> dataServerTypeMap = RedisUtils.getCacheObject(DATA_SERVER_TYPE);
        if (CollUtil.isEmpty(dataServerTypeMap)) {
            loadDataServerType();
            dataServerTypeMap = RedisUtils.getCacheObject(DATA_SERVER_TYPE);
        }
        return dataServerTypeMap.get(name);
    }
    @Override
    public List<CascaderModel> dataServerSelect() {
        List<DataServer> dataServers = this.list();
        List<CascaderModel> list = Lists.newArrayList();
        if (CollUtil.isNotEmpty(dataServers)) {
            Map<String, List<DataServer>> dataServerGroup = dataServers.stream().collect(Collectors.groupingBy(DataServer::getServerType));
            for (Map.Entry<String, List<DataServer>> entry : dataServerGroup.entrySet()) {
                CascaderModel cascaderModel = new CascaderModel();
                Long typeId = getTypeId(entry.getKey());
                cascaderModel.setValue(typeId);
                cascaderModel.setLabel(entry.getKey());
                List<CascaderModel> childrenList = Lists.newArrayList();
                for (DataServer dataServer : entry.getValue()) {
                    CascaderModel children = new CascaderModel();
                    children.setValue(dataServer.getId());
                    children.setLabel(dataServer.getServerName());
                    childrenList.add(children);
                }
                cascaderModel.setChildren(childrenList);
                list.add(cascaderModel);
            }
        }
        return list;
    }

}
